 var path = require('path');

 var fs = require('fs');



 var util = require('util');

 var mslibrary = require('ms-common-library');


 var openjson = mslibrary.openjson;
 var RemoveFile = function (File) {
     fs.exists(File, function (exists) {
         if (exists) {
             var shell = require('shelljs');
             shell.rm('-rf', File);
         }
     })
 }

 module.exports = function (source, body, config, Callback) {

    var value = {
        error: 108
    };
var localpath=body.localpath;

     var AdmZip = require('adm-zip');

     var targetPath = config.Files.Plugin;

     var PluginPath = path.join(targetPath, localpath)

     // reading archives
     var zipfile=body.zipfile;
     var zip = new AdmZip(zipfile);

     var zipEntries = zip.getEntries(); // an array of ZipEntry records
     var off = false;
     zipEntries.forEach(function (zipEntry) {
         if (zipEntry.entryName.toString() == "package.json") {
             off = true;
         }
     });
     if (off) {
         if (!fs.existsSync(PluginPath)) {
             fs.mkdirSync(PluginPath);
         };
         zip.extractAllTo(PluginPath, true);


         var shell = require('shelljs');

         RemoveFile(zipfile);
         openjson(path.join(PluginPath, 'package.json'), function (error, db) {
             if (error == null) {
                 var obj = {
                     author: db.author,
                     name: db.label,
                     icon: 26,
                     user_id: body.user_id,
                     key: db.id,
                     description: db.description,
                     localpath: localpath,
                     
                 }
                 new source("M_674", {
                     id: db.id
                 }, function (Olddb) {

                     value.error = 0;
                     value.id = 100;
                     var src = 'Plugin/' + db.id + ".png";

                     var Pluginlogo = path.join(config.Files.FileCache, src);

                     shell.cp('-R', path.join(PluginPath, 'logo.png'), Pluginlogo);

                     if (Olddb == null) {
                         var info = {
                             src: src,
                             title: db.label,
                             user_id: body.user_id,
                             fileformat: 'png',
                             FileType: 4,
                             
                         };

                         new source("M_621", info, function (img_info_db) {
                             if (img_info_db && img_info_db.id) {
                                 obj.icon = img_info_db.id;
                             };
                             new source("M_669", obj, function (db) {
                                
                                 Callback(value,db?db.id:null);
                             })
                         })
                     } else {
                         RemoveFile(path.join(config.Files.Plugin, Olddb.localpath));
                         obj.id = Olddb.id;
                         new source("M_675", obj, function (db) {
                             Callback(value);
                         })
                     }
                 })

             } else {
                 RemoveFile(PluginPath);
                 value.error = 1;
                 Callback(value);
             }
         })
     } else {

         Callback(value);
     }
 }